﻿Imports System.Management

Module Module1

    Sub Main()

        Console.WriteLine("Bem Vindo, esta aplicação irá criar/apagar um Log no Event Viewer")
        Console.WriteLine()
        Console.WriteLine("Escolha a operação que deseja executar: 1 - criar, 2 - apagar")
        Dim _operacao As Integer = Console.ReadLine

        Select Case _operacao

            Case 1
                Console.Write("Insira o nome que o Log irá ter: ")
                Dim _LogName As String = Console.ReadLine
                Console.Write("Insira o nome da aplicação que irá escrever no Log: ")
                Dim _SourceName As String = Console.ReadLine

                Dim resultado As String = EscreveEventViewer(_LogName, _SourceName, _operacao)

                Console.WriteLine()
                Console.WriteLine(resultado)

            Case 2
                Console.Write("Insira o nome do Log a apagar: ")
                Dim _LogName As String = Console.ReadLine
                Console.Write("Insira o nome da aplicação a apagar: ")
                Dim _SourceName As String = Console.ReadLine

                Dim resultado As String = EscreveEventViewer(_LogName, _SourceName, _operacao)

                Console.WriteLine()
                Console.WriteLine(resultado)

            Case Else

        End Select

    End Sub

    Private Function EscreveEventViewer(ByVal LogName As String, _
                                        ByVal SourceName As String, _
                                        ByVal Operacao As Integer) As String

        Try
            Select Case Operacao

                Case 1

                    If Not EventLog.SourceExists(SourceName) Then
                        EventLog.CreateEventSource(SourceName, LogName)
                    End If

                    Dim elLog As New System.Diagnostics.EventLog(LogName, Environment.MachineName, SourceName)
                    elLog.WriteEntry("Log criado com sucesso: " & Date.Now, EventLogEntryType.Information)
                    elLog.Dispose()

                    Return "Operação executada com sucesso!"

                Case 2

                    If EventLog.Exists(LogName) Then
                        EventLog.Delete(LogName)
                    End If

                    If EventLog.SourceExists(SourceName) Then
                        EventLog.DeleteEventSource(SourceName)
                    End If

                    Return "Operação executada com sucesso!"

                Case Else

                    Return "Escolha uma operação válida!"

            End Select

        Catch ex As Exception

            Return ex.Message.ToString

        End Try

    End Function

#Region "Cenas Comentadas"

    ''Write an entry to the Application Log. Notice the use of "DemoEventLog", this is the source that will be used
    ''for this application and is used to state what application/service the log refers too.

    ''EventLog.CreateEventSource("VG3", "Vasco Da Gama V3")
    'Dim elLog As New System.Diagnostics.EventLog("Application", Environment.MachineName, "DemoEventSource")
    'Dim elLog As New System.Diagnostics.EventLog("VascoDaGamaV3", Environment.MachineName, "VascoDaGamaV3")
    ' ''Dim elLog As New System.Diagnostics.EventLog("VG3Log", Environment.MachineName, "VG")

    'elLog.WriteEntry("This is a information message", EventLogEntryType.Information)
    'elLog.WriteEntry("This is a warning message", EventLogEntryType.Warning)
    'elLog.WriteEntry("This is a Error message", EventLogEntryType.Error)
    'elLog.WriteEntry("This is a Success Audit message", EventLogEntryType.SuccessAudit)
    'elLog.WriteEntry("This is a Failed Audit message", EventLogEntryType.FailureAudit)

    'elLog.Dispose()

    ''If EventLog.SourceExists("VG3") Then
    'EventLog.CreateEventSource("VascoDaGamaV3", "VascoDaGamaV3")
    'EventLog.CreateEventSource("VG", "VG3Log")
    'EventLog.DeleteEventSource("VG3")
    'EventLog.Delete("VG3Log")
    'EventLog.Delete("VascoDaGamaV3")
    ''End If

    'Dim source As New EventSourceCreationData("VascoDaGamaV3", "VascoDaGamaV3")
    'source.MachineName = "ATALANTA"

    'System.Diagnostics.EventLog.CreateEventSource(source)

#End Region

End Module
